// grunt入口文件
// 导出一个函数
const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')

module.exports = grunt => {
    grunt.initConfig({
        clean: {
            temp: ['dist/**','temp/**']
        },
        // sass->css
        sass: {
            options: {
                // sourceMap: true,
                implementation: sass
            },
            main: {
                files: [{
                    expand: true,
                    cwd: 'src/assets/styles',
                    src: ['*.scss'],
                    dest: 'temp/assets/styles',
                    ext: '.css'
                }]
            }
        },
        // 编译ES6语法
        babel: {
            options: {
                // sourceMap: true,
                presets: ['@babel/preset-env'], //加载最新的ECMAscript特性
            },
            main: {
                files: [{
                    expand: true,
                    cwd: 'src/assets/scripts',
                    src: ['*.js'],
                    dest: 'temp/assets/scripts',
                    ext: '.js'
                }]
            }
        },
        // 编译html模板文件
        web_swig: {
            main: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ['*.html'],
                    dest: 'temp',
                    ext: '.html'
                }]
            }
        },
        // 图片转换,字体文件转换
        imagemin: {
            options: {
                optimizationLevel: 3,
                // pngquant: true
            },
            main: {
                files: [{
                    expand: true,
                    cwd: 'src/assets',
                    src: ['**/*.{svg, png, jpg, jpeg}'], //现在只能压缩svg,png的没有输出文件
                    dest: 'dist/assets'
                }]
            }
        },
        // 监听文件变化
        watch: {
            options: {
                livereload: true
            },
            js: {
                files: ['src/assets/scripts/*.js'],
                tasks: ['babel']
            },
            css: {
                files: ['src/assets/styles/*.scss'],
                tasks: ['sass']
            },
            html: {
                files: ['src/*.html'],
                tasks: ['web_swig']
            }
        },
        connect: {
            server: {
                options: {
                    port: 8080,
                    hostname: '*',
                    livereload: true,
                    // keepalive: true,
                    open: true,
                    base: ['temp', 'src', 'pubilc']
                }
            }
        },
        // 额外的文件
        copy: {
            main: {
                files: [{
                    expand: true,
                    cwd: 'public',
                    src: ['**'],
                    dest: 'dist/public'
                }]
            }
        },
        // html压缩
        htmlmin: {
            options: {
                removeComments: true,//移除注释
                collapseWhitespace: true//移除空格换行
            },
            main: {
                files: [{
                    expand: true,
                    cwd: 'temp',
                    src: ['*.html'],
                    dest: 'dist'
                }]
            }
        },
        // css压缩
        cssmin: {
            options: {
                mergeIntoShorthands: false,
                roundingPrecision: -1
            },
            target: {
                files: [{
                    expand: true,
                    cwd: 'temp',
                    src: ['**/*.css'],
                    dest: 'dist',
                    ext: '.min.css'
                }]
            }
        },
        //js压缩
        uglify: {
            main: {
                files: [{
                    expand: true,
                    cwd: 'temp',
                    src: ['**/*.js'],
                    dest: 'dist',
                    ext: '.min.js'
                }]
            }
        },
        // 合并
        useref: {
            html: 'temp/*.html',
            temp: 'dist'
        }
    })

    loadGruntTasks(grunt)   //加载所有grunt插件中的任务

    grunt.registerTask('develop', ['sass', 'babel', 'web_swig', 'useref', 'connect', 'watch'])
    grunt.registerTask('default', ['clean', 'sass', 'babel', 'web_swig', 'useref', 'copy', 'htmlmin', 'cssmin', 'uglify', 'imagemin'])
} 